package com.tencent.could.ocrdemo.demo;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.ahrykj.weddingcartaxi.R;
import com.google.gson.Gson;
import com.rykj.base.BaseActivity;
import com.tencent.could.ocrdemo.model.Drivers;
import com.tencent.could.ocrdemo.model.TrainTicket;
import com.tencent.could.ocrdemo.utils.AuthService;
import com.tencent.could.ocrdemo.utils.CommonUtil;
import com.tencent.could.ocrdemo.utils.CustomConfigUtil;
import com.tencent.could.ocrdemo.utils.DrivingLicense;

import java.io.File;

public class TrainTicketActivity extends BaseActivity {

    Button btnTake;
    Button btnSelect, btn_save;
    ImageView ivShow;
    Button btnrh;
    Bitmap bitmap = null;
    File photoFile;
    TextView textView;

    private Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message message) {
            if (message.what == 1) {
                showLoading("证件识别中");
                DrivingLicense.trainTicket(message.obj.toString(), photoFile.getPath(), handler);
            } else if (message.what == 2) {
                disMissLoading();
                TrainTicket drivers = new Gson().fromJson(message.obj.toString(), TrainTicket.class);
                textView.setText("编号：" + drivers.words_result.ticket_num + "\n"
                        + "出发站：" + drivers.words_result.starting_station + "\n"
                        + "到达站：" + drivers.words_result.destination_station+ "\n"
                        + "出发时间：" + drivers.words_result.date + "\n"
                        + "车次：" + drivers.words_result.train_num + "\n"
                        + "座位号：" + drivers.words_result.seat_num + "\n"
                        + "姓名：" + drivers.words_result.name + "\n"
                        + "票价：" + drivers.words_result.ticket_rates + "\n"
                        + "席别：" + drivers.words_result.seat_category + "\n");
            }
            return false;
        }
    });

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        btnTake = (Button) findViewById(R.id.btn_take);
        btnSelect = (Button) findViewById(R.id.btn_select);
        ivShow = (ImageView) findViewById(R.id.iv_show);
        btnrh = (Button) findViewById(R.id.btn_rh);
        btn_save = (Button) findViewById(R.id.btn_save);
        textView = findViewById(R.id.text);
        photoFile = new File(getExternalFilesDir("img"), "scan.jpg");

        btnTake.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivityForResult(CropActivity.getJumpIntent(TrainTicketActivity.this, false, photoFile), 100);
            }
        });

        btnSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // AuthService.getAuth(handler);
                startActivityForResult(CropActivity.getJumpIntent(TrainTicketActivity.this, true, photoFile), 100);
            }
        });
        btnrh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (bitmap != null) {
                    ivShow.setImageBitmap(CustomConfigUtil.getInstance().sharpenImageAmeliorate(bitmap));
                } else {
                    Toast.makeText(TrainTicketActivity.this, "请先选择图片或者拍照证件", Toast.LENGTH_SHORT).show();
                }
            }
        });

        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                if (bitmap != null) {
                    CommonUtil.saveBitmap2file(bitmap, getApplicationContext());
                    Toast.makeText(TrainTicketActivity.this, "图片保存成功，请在相册查看", Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(TrainTicketActivity.this, "请先选择图片或者拍照证件", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_drivers_license;
    }

    @Override
    public boolean immersionEnable() {
        return false;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK) {
            return;
        }
        if (requestCode == 100 && photoFile.exists()) {

            bitmap = BitmapFactory.decodeFile(photoFile.getPath());
            AuthService.getAuth(handler);
            ivShow.setImageBitmap(bitmap);
        }
    }
}
